<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">

        <el-form-item label="试题题目" prop="className">
          <el-input
            v-model="queryParams.questionsName"
            placeholder="请输入试题题目"
            clearable
            size="small"
            style="width: 180px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="试题类型" prop="status">
          <el-select v-model="queryParams.questionsType" style="width: 180px">
            <el-option label="单选题" value="1"></el-option>
            <el-option label="多选题" value="2"></el-option>
            <el-option label="判断题" value="3"></el-option>
            <el-option label="填空题" value="4"></el-option>
            <el-option label="问答题" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用专业" prop="status">
          <el-select v-model="queryParams.major" clearable filterable >
              <el-option v-for="item in majorList" :label="item.subjectName+''" placeholder="班级专业"
                  :value="item.id+''" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用科目" prop="status">
          <el-select v-model="queryParams.subjects" clearable filterable size="small" >
              <el-option v-for="dict in subjectList" :key="dict.id" :label="dict.subjName" :value="dict.id+''" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用年级" prop="status">
          <el-select v-model="queryParams.grade" clearable filterable >
              <el-option v-for="dict in classGradeList" placeholder="班级年级"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue+''" ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增试题</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
            @click="importData"
            >批量导入试题</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="bclassList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="适用专业" align="center" prop="major" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            {{ getMajorLable(scope.row.major) }}
          </template>
        </el-table-column>
        <el-table-column label="适用科目" align="center" prop="subjects" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            {{ getSubjectLable(scope.row.subjects) }}
          </template>
        </el-table-column>
        <el-table-column label="试题类型" align="center" prop="questionsType" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <label v-if="scope.row.questionsType==1">单选题</label>
            <label v-if="scope.row.questionsType==2">多选题</label>
            <label v-if="scope.row.questionsType==3">判断题</label>
            <label v-if="scope.row.questionsType==4">填空题</label>
            <label v-if="scope.row.questionsType==5">问答题</label>
          </template>
        </el-table-column>
        <el-table-column label="试题题目" align="center" prop="questionsName" width="160" :show-overflow-tooltip="true" >
        </el-table-column>
        <el-table-column label="适用年級" align="center" prop="grade" width="160">
          <template slot-scope="scope">
            {{ selectDictLabel(classGradeList, scope.row.grade) }}
          </template>
        </el-table-column>
        <el-table-column label="分数分数" align="center" prop="sore" width="160">
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createBy" width="100">
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="modifyTime" width="160">
            <template slot-scope="scope">
            {{ parseTime(scope.row.modifyTime) }}
          </template>
        </el-table-column>
        <el-table-column label="更新人" align="center" prop="modifyBy" width="100">
        </el-table-column>

        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    circle type="success"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"

            />
            <el-button
                    size="mini"
                    circle type="danger"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"

            />
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <el-dialog title="导入试题信息" :visible.sync="importVisible" width="400px" >
        <el-upload v-loading="importLoading"
          class="upload-demo"
          drag
          :action="upload.url"
          :headers="upload.headers"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :file-list="fileList"
          >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip"><el-link type="primary" :href="templateDowloadUrl">模板下载</el-link></div>
        </el-upload>
      </el-dialog>
    </div>
  </template>

  <script>
  import { findSchoolClassPageList, addOrUpdateSchoolClassInfo, deleteSchoolClassInfo, exportBClass, batchUpdateClassStatus} from "@/api/classManage/classInfo";
  import { pageList, editExam, deleteExam,deleteBatchExam,updateBatchExam} from "@/api/studentInfo/examManage";
  import { findBaseSpaceLocationPageList} from "@/api/baseData/siteManagement";
  import { getClassTeacherList } from "@/api/system/user";
  import { findBaseSubjectPageList} from "@/api/baseData/profession";
  import { listTermtime } from "@/api/system/termtime.js";
  import { tExamSubjectPageList,deleteQuestions} from "@/api/studentInfo/questions";
  import {subjectList} from "@/api/educational/subject.js";
  import { getToken } from '@/utils/auth'


  export default {
    name: "BClass",
    data() {
      return {
        importLoading:false,
        templateDowloadUrl: process.env.VUE_APP_BASE_API + "/school/file/examTemp.xlsx",
        upload:{
          url:process.env.VUE_APP_BASE_API + '/tExamSubject/tExamSubjectExcel',
          headers: {Authorization:"Bearer " + getToken()},
        },
        importVisible:false,
        fileList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        rowList: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        stuCount: 0,
        // 字典表格数据
        bclassList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 班主任
        userOptions: [],
        // 状态数据字典
        statusOptions: [],
        // 班级所属部门
        classDeptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          className: undefined,
          status: undefined,
          classDept:undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          examName: [
            { required: true, message: "考试名称不能为空", trigger: "blur" }
          ],
          examBeginTime: [
            { required: true, message: "考试开始时间不能为空", trigger: "blur" }
          ],
          examEndTime: [
            { required: true, message: "考试结束时间不能为空", trigger: "blur" }
          ],
          classId:[
            { required: true, message: "考试班级不能为空", trigger: "blur" }
          ],
          classRoomId:[
            { required: true, message: "考试教室不能为空", trigger: "blur" }
          ],
          curriculumType:[
            { required: true, message: "考试课程不能为空", trigger: "blur" }
          ]
        },
        subjectList:[],
        majorList:[],//专业
        termTimeList:[],
        courseTypeOptions:[],
        classOptions:[],
        classGradeList:[]
      };
    },
    created() {
      this.getMajortList()
      this.getList();
      this.getClassTeacherList();
      this.getSubjectList()
      this.getTermTimeList()
      // this.getClassList()

      this.getDicts("class_grade").then(response => {
        //年级
        this.classGradeList = response.data;
      });

    },
    methods: {
      importData(){
        //打开导入页面
        this.importVisible = true
        this.fileList = []

      },
      handleAvatarSuccess(res, file){
        this.fileList.push(file)
        if(res.code==200){
          this.$message({ message: '导入成功', type: 'success'});
          this.getList()
          this.importLoading = false
        }else{
          this.$message.error(res.message);
          this.importLoading = false
        }
        this.fileList=[]
      },
      beforeAvatarUpload(file){
        this.importLoading = true
        console.log(file.name.lastIndexOf('.xlsx'))
        if(file.name.lastIndexOf('.xlsx')<0 && file.name.lastIndexOf('.XLSX')<0){
          this.$message({
            message: '上传文件只能是.xlsx文件',
            type: 'warning'
          });
          return false
        }
        console.log(file,'beforefile')

      },
        getClassRoom(){
            findBaseSpaceLocationPageList({pageNum:1,pageSize:1000,locationType:4}).then(response => {
                this.classRoomOptions = response.data.list;
            });
        },
        getClassList(){
            //查询班级
            findSchoolClassPageList({pageSize:500,pageNum:1,status:1,isGraduation:0}).then(resp=>{
                this.classOptions = resp.data.list
            })
        },
        getTermTimeList() {
            //查询学期
            this.loading = true;
            listTermtime({pageSize:100,pageNum:1,status:1}).then(response => {
                this.termTimeList = response.data.list;

                }
            );
        },
      getMajortList(){
        //查询科目
        findBaseSubjectPageList({pageSize:100,pageNum:1,status:1}).then(response =>{
            this.majorList = response.data.list
        })
      },
      getSubjectList(){
            //查询专柜
        subjectList({pageSize:100,pageNum:1,status:1}).then(response =>{
            this.subjectList = response.data.list
        })
      },
      getMajorLable(val){
        for(let i=0;i<this.majorList.length;i++){
          if(this.majorList[i].id==val){
            return this.majorList[i].subjectName
          }
        }
      },
      getSubjectLable(val){
        for(let i=0;i<this.subjectList.length;i++){
          if(this.subjectList[i].id==val){
            return this.subjectList[i].subjName
          }
        }
      },
      /** 查询班级列表 */
      getList() {
        this.loading = true;
        console.log(this.queryParams)
        tExamSubjectPageList(this.queryParams).then(response => {
            this.bclassList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      getClassTeacherList(){
        getClassTeacherList().then(response => {
          this.userOptions = response.data;
        });
      },
      // 字典设备状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 字典设备状态字典翻译
      classDeptFormat(row, column) {
        return this.selectDictLabel(this.classDeptOptions, row.classDept);
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          className: undefined,
          classTeacherId: undefined,
          status: "1",
          classDept:undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams={
          pageSize:10,
          pageNum:1,
          questionsName:null,
          questionsType:null,
          major:null,
          subjects:null,
          grade:null
        }
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {

        this.$router.push("/educational/exam/editQusetions");
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        console.log(selection)
        this.rowList = selection

        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$router.push({path:"/educational/exam/editQusetions",query:{id:row.id}})
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除数据项?', "警告", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                }).then(function() {
                  return deleteQuestions(row.id);
                }).then(() => {
                  this.getList();
                  this.msgSuccess("删除成功");
                }).catch(function() {});
      },

      // 获取当前选中班主任的名称
      selectUser(val) {
        alert(val.label);
        this.form.username = val.label
      },


    }
  };
  </script>
